{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "# checking python version, we need 3.11+\n",
        "!python3.11 --version\n",
        "!pip3 --version"
      ],
      "metadata": {
        "id": "xo0QI38L7QsF"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# install python3.11 and pip\n",
        "!apt-get install python3.11 python3.11-distutils\n",
        "!curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11"
      ],
      "metadata": {
        "id": "zly0aU8B7GdZ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "rCp_mqGLvJPT"
      },
      "outputs": [],
      "source": [
        "# Install testzeus-hercules from PyPI\n",
        "!pip3 install testzeus-hercules\n",
        "\n",
        "# Install Playwright and its dependencies\n",
        "!pip3 install playwright\n",
        "!playwright install --with-deps"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Set the LLM model and API key\n",
        "llm_model = \"gpt-4o\"  # Replace with your model name\n",
        "llm_model_api_key = \"sk-proj-.......\"  # Replace with your API key"
      ],
      "metadata": {
        "id": "rbTNgolryUKC"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "import tempfile\n",
        "\n",
        "# Create temporary directories for input, output, and test data\n",
        "project_base = tempfile.mkdtemp(prefix=\"hercules_project_\")\n",
        "input_dir = os.path.join(project_base, \"input\")\n",
        "test_data_dir = os.path.join(project_base, \"test_data\")\n",
        "output_dir = os.path.join(project_base, \"output\")\n",
        "\n",
        "os.makedirs(input_dir, exist_ok=True)\n",
        "os.makedirs(output_dir, exist_ok=True)\n",
        "os.makedirs(test_data_dir, exist_ok=True)\n",
        "\n",
        "# Create a sample Gherkin feature file\n",
        "feature_content = \"\"\"\n",
        "Feature: Open Google homepage\n",
        "\n",
        "  Scenario: User opens Google homepage\n",
        "    Given I have a web browser open\n",
        "    When I navigate to \"https://www.google.com\"\n",
        "    Then I should see the Google homepage\n",
        "\"\"\"\n",
        "\n",
        "feature_file_path = os.path.join(input_dir, \"test.feature\")\n",
        "with open(feature_file_path, \"w\") as feature_file:\n",
        "    feature_file.write(feature_content)\n",
        "\n",
        "print(f\"Project base directory: {project_base}\")\n",
        "print(f\"Feature file created at: {feature_file_path}\")\n"
      ],
      "metadata": {
        "id": "mGfEXUZSxpUe"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "with open(feature_file_path, \"r\") as fp:\n",
        "  print(fp.read())"
      ],
      "metadata": {
        "id": "NPO8NZvPxt9K"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import subprocess\n",
        "\n",
        "# Define the command to run Hercules\n",
        "command = \" \".join([\n",
        "    \"testzeus-hercules\",\n",
        "    \"--project-base\", project_base,\n",
        "    \"--llm-model\", llm_model,\n",
        "    \"--llm-model-api-key\", llm_model_api_key\n",
        "])\n",
        "try:\n",
        "  # Execute the command\n",
        "  result = subprocess.run(\n",
        "              command,\n",
        "              check=True,\n",
        "              env=dict(os.environ),\n",
        "              capture_output=True,\n",
        "              encoding=\"utf-8\",\n",
        "              text=True,\n",
        "              errors=\"replace\", shell=True\n",
        "          )\n",
        "\n",
        "  # Display the output and errors, if any\n",
        "  print(\"Hercules execution output:\")\n",
        "  print(f\"Standard Output:\\n{result.stdout}\")\n",
        "  print(f\"Standard Error:\\n{result.stderr}\")\n",
        "  print(f\"Return Code: {result.returncode}\")\n",
        "except subprocess.CalledProcessError as e:\n",
        "    print(e.returncode)\n",
        "    print(e.cmd)\n",
        "    print(e.output)\n",
        "    print(e.stderr)\n",
        "    print(e.stdout)"
      ],
      "metadata": {
        "id": "IOLKB9swxw2a"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "\n",
        "# List the contents of the output directory\n",
        "print(\"Output directory contents:\")\n",
        "print(\"The output is: \")\n",
        "path = os.path.join(output_dir, \"test.feature_result.xml\")\n",
        "with open(path, 'r') as fp:\n",
        "  print(fp.read())\n"
      ],
      "metadata": {
        "id": "CyaVaDtjxzdR"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}